package BasicKnowledge.Basic.IO;

import java.io.*;

/**
 * @time: 2021/2/2/002 17:04
 * @author: Zhurunhe
 */
public class Demo9 {
    public static void main(String[] args) throws IOException {
        String path = "G:/abc.txt";
        String topath = "G:/bcd.txt";
        //testBufferedReader(path);
        //testBufferedWriter(topath);
        testWriterToTxt(topath);

    }

    private static void testWriterToTxt(String topath) {
        BufferedWriter bufferedWriter= null;
        BufferedReader bufferedReader= null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(topath));
            //使用BufferedReader对键盘输入到缓冲中
            bufferedReader = new BufferedReader(new InputStreamReader(System.in));

            String s=bufferedReader.readLine();
            while (s.length()>0){
                bufferedWriter.write(s);
                bufferedWriter.newLine();
                s=bufferedReader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }

    private static void testBufferedWriter(String topath) {
        BufferedWriter bufferedWriter= null;
        try {
            Writer writer=new FileWriter(topath,true);
            bufferedWriter = new BufferedWriter(writer);

            bufferedWriter.write("BufferedWriter测试");
            bufferedWriter.write("\r");
            bufferedWriter.write("\n");
            bufferedWriter.write("测试中文");//会出现中文乱码
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter!=null){
                try {
                    bufferedWriter.flush();//清空缓冲区，让写入缓冲区的内容发送到文件中
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }

    private static void testBufferedReader(String path)  {
        BufferedReader bufferedReader = null;
        try {
            Reader reader = new FileReader(path);
            bufferedReader = new BufferedReader(reader);

            String s = bufferedReader.readLine();//读取一行，读到文末尾返回null
            while (s != null) {
                System.out.println(s);
                s = bufferedReader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

